package com.ymatou.iapi.sellercs.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellercs.parameter.SendHeartbeatToPcBean;
import com.ymatou.iapi.sellercs.service.SendHeartbeatToPcCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.GetLog;
import com.ymttest.utils.TestDataManager;
/**
*向PC网页端发送心跳
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendHeartbeatToPc{
	private static SendHeartbeatToPcBean sendheartbeattopcBean;
	private static SendHeartbeatToPcCall sendheartbeattopcCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("向PC网页端发送心跳");
	}

	@Before
	public void caseUp() {
		sendheartbeattopcBean=new SendHeartbeatToPcBean();
		sendheartbeattopcCall=new SendHeartbeatToPcCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendheartbeattopcCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendHeartbeatToPc_001() {
		Logger.start(true,"向PC网页端发送心跳,成功");
		try {
			String token=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(sellerId, sellerId, token);
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			getLog.startListenVm("default.log",".");
			sendheartbeattopcCall.setData(sendheartbeattopcBean);
			sendheartbeattopcCall.callService();			
			Logger.verifyEquals("ok", sendheartbeattopcCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			loglist.forEach(log->{Logger.debug(log);});
			//验证日志
			Logger.comment("验证日志包含发送指令");
			List<String> filter2 = loglist.stream().filter(s->s.contains("发送心跳数:")).collect(Collectors.toList());
			Logger.verifyEquals(1, filter2.size(), "验证日志数量");
			String [] msg=filter2.get(0).split(":");
			Integer count=Integer.valueOf(msg[msg.length-1]);
			Logger.verifyEquals(true, count>=1, "验证发送心跳数");
			//Thread:main 2018-10-19 11:13:18.042 2018-10-19 11:13:16,699 [taskExecutor-4] INFO  c.y.sellercs.domain.service.JobService KEY[SendHeartbeatToPcReq|22ba3d32e4a24d15be38b5751e097c29] [fa83abf1014ae47e] - 向PC登录的客服发心跳方法执行结束，发送心跳数:1
			SellerCSIMCallService.OffLine(sellerId, token);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}